package com.jimmy.medical.service.impl;

import com.jimmy.medical.entity.*;
import com.jimmy.medical.enums.OrderStatusEnum;
import com.jimmy.medical.enums.PictureUseTypeEnum;
import com.jimmy.medical.repository.TakeNumRecordRepository;
import com.jimmy.medical.service.ITakeFlowOrderService;
import com.jimmy.medical.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
@Qualifier("takeFlowOrderService")
public class TakeFlowOrderServiceImpl extends FlowOrderServiceImpl
        implements ITakeFlowOrderService {

    @Autowired
    private TakeNumRecordRepository takeNumRecordRepository;

    @Override
    public Long saveAcceptInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "接号", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话通知", 2);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getFlowOrderId(), message.getUserId(), "短信告知", 3);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveAppearance(Long flowOrderId, Long userId, Long photoId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "自检仪容", 4);

        UploadFile photo = uploadFileRepository.findOne(photoId);
        photo.setUseType(PictureUseTypeEnum.CHECK_APPEARANCE.getKey());
        photo.setRelatedKey(flowOrder.getOrder().getId().toString());
        uploadFileRepository.save(photo);

        return getCountDown(flowOrder);
    }

    @Override
    public Long saveFeeProcess(Long flowOrderId, Long userId) {
        return saveFeeProcess(flowOrderId, userId, 5);
    }

    @Override
    public Long saveTakeNoInfo(Long flowOrderId, Long userId, String content) {
        return saveTakeNoInfo(flowOrderId, userId, content, true);
    }

    @Override
    public Long saveTakeNoInfo(Long flowOrderId, Long userId, String content, boolean processFlow) {
        FlowOrder flowOrder;
        if(processFlow) {
            flowOrder = process(flowOrderId, userId, "取号", 6);
        } else {
            flowOrder = repository.findOne(flowOrderId);
        }

        User user = userRepository.findOne(userId);
        TakeNumRecord record = takeNumRecordRepository.findFirstByOrder(flowOrder.getOrder());
        if(record == null) {
            record = new TakeNumRecord();
            record.setCreateTime(CommonUtils.getNowTimeShortString());
            record.setCreateUser(user.getRealname());
        }

        record.setInfo(content);
        record.setIsTake(1);
        record.setIsDiagnose(0);
        record.setOrder(flowOrder.getOrder());
        takeNumRecordRepository.save(record);
        return getCountDown(flowOrder);
    }

    public TakeNumRecord getTakeNumRecord(Long flowOrderId) {
        FlowOrder flowOrder = repository.findOne(flowOrderId);
        return takeNumRecordRepository.findFirstByOrder(flowOrder.getOrder());
    }

    @Override
    public FlowOrder submitTake(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成取号", 7);
        return getNextFlowOrder(flowOrder);
    }
}